﻿using System;
using System.IO;
using System.Windows.Forms;

namespace Compiler
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Compiler.CompilerInit();
            ConsolePrint("请选择测试数据");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            tbConsole.ReadOnly = true;
        }

        private void OnBtnClick_Clear(object sender, EventArgs e)
        {
            ConsoleClear();
            Compiler.CompilerInit();
        }

        private void OmCbxChanged_Input(object sender, EventArgs e)
        {
            Compiler.CompilerInit();
            LoadInput(cbxInput.SelectedIndex);
        }
        private void OnbtnClick_Lex(object sender, EventArgs e)
        {
            ConsolePrint("词法分析");
            LexicalAnalysis.Analyse();
        }

        private void OnbtnClick_Syn(object sender, EventArgs e)
        {
            ConsolePrint("语法分析");
            SyntaxAnalysis.Analyse();
        }

        private void OnBtnClick_Qua(object sender, EventArgs e)
        {
            ConsolePrint("中间代码");
            Quaternion.PrintQuaternions();
        }

        #region 对外接口
        /// <summary>
        /// 控制台追加输出
        /// </summary>
        public void ConsoleAppend(string outputLine)
        {
            tbConsole.AppendText(outputLine + Environment.NewLine);
        }
        /// <summary>
        /// 控制台重置后输出
        /// </summary>
        public void ConsolePrint(string outputLine)
        {
            ConsoleClear();
            tbConsole.AppendText(outputLine + Environment.NewLine);
        }
        /// <summary>
        /// 控制台重置
        /// </summary>
        public void ConsoleClear()
        {
            tbConsole.Text = string.Empty;
            tbDebug.Text = string.Empty;
        }
        /// <summary>
        /// 异常日志
        /// </summary>
        public void DebugLog(string outputLine)
        {
            tbDebug.AppendText(outputLine + Environment.NewLine);
        }
        public void LoadInput(int fileId)
        {
            string path = $"测试输入{fileId}:";
            ConsolePrint(path);
            try
            {
                switch (fileId)
                {
                    case -1: throw new Exception("未选择输入文件");
                    case 0: path = "../../Files/input0.txt"; break;
                    case 1: path = "../../Files/input1.txt"; break;
                    case 2: path = "../../Files/input2.txt"; break;
                    case 3: path = "../../Files/input3.txt"; break;
                    case 4: path = "../../Files/input4.txt"; break;
                    default: throw new Exception("未选择输入文件");
                }

                using (StreamReader reader = new StreamReader(path))
                {
                    for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                    {
                        ConsoleAppend(line);
                        LexicalAnalysis.SourceCode += line;
                    }
                }
            }
            catch (Exception e)
            {
                Compiler.Log(e);
                ConsolePrint("请选择测试数据");
                return;
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            SyntaxHelper.ProductionToAnalyzeTable();
        }
    }
}
